﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using control.Dal;
using System.Threading;

namespace control.Biz
{
    public class BizDonHang
    {
        public int ID { get; set; }
        public string Ma { get; set; }
        public DateTime ThoiGian { get; set; }
        public int TrangThai { get; set; }
        public string TenTT
        {
            get
            {
                BizTrangThai trangthai = BizTrangThai.GetByID(TrangThai);
                if (trangthai != null)
                    return trangthai.TrangThai;
                else
                    return "";
            }
        }
        public string HoTen { get; set; }
        public string SoDienThoai { get; set; }
        public string Email { get; set; }
        public string SoTaiKhoan { get; set; }

        private List<BizChiTietDonHang> items;
        public List<BizChiTietDonHang> Items
        {
            set { items = value; }
            get
            {
                if (items == null)
                    items = new List<BizChiTietDonHang>();
                return items;
            }
        }

        public int FindIndexItem(int ID)
        {
            for (int i = 0; i < Items.Count; i++)
            {
                if (Items[i].SanPham.ID == ID)
                    return i;
            }
            return -1;
        }

        public int TongTien
        {
            get
            {
                decimal resuilt = 0;
                foreach (BizChiTietDonHang item in Items)
                {
                    resuilt += item.ThanhTien;
                }
                return (int)resuilt;
            }
        }

        public void Insert()
        {
            ID = DalDonHang.Insert(this);

        }


        public static List<BizDonHang> GetAll()
        {
            return DalDonHang.GetAll();
        }

        public static BizDonHang GetByID(int ID)
        {
            return DalDonHang.GetByID(ID);
        }

        public bool Detele()
        {
            return DalDonHang.Delete(this);
        }

        public static bool KiemTraSotk(string sotk)
        {
            return DalDonHang.KiemTraSotk(sotk);
        }

        public void Update()
        {
            DalDonHang.Update(this);
        }
    }
}
